/**
 * @class 网络管理器
 * @author DuskyHuang 圣子
*/

import { singleton } from '../utils/decorators';
import UIManager from './ui-manager';
export enum Domain { PROD, DEV, TEST, LOCAL }

@singleton
export default class NetManager {

    declare public static readonly instance: NetManager;
    public static readonly Domain: typeof Domain = Domain;
    public useReport: boolean = false;
    public useCover: boolean = true;
    public simulateLatency: boolean = false;
    public latency: time.sec = 0.2;
    public logReq: boolean = false;
    public logResp: boolean = true;
    public domain: Domain = Domain.PROD;
    public uncovers: string[] = [];
    public get apis(): object {
        switch (this.domain) {
            case Domain.PROD: return this._apis.prod;
            case Domain.DEV: return this._apis.dev;
            case Domain.TEST: return this._apis.test;
            case Domain.LOCAL: return this._apis.local;
        }
    }
    public get isProduce(): boolean { return this.domain === Domain.PROD; }
    public setApis(apis: object): this { this._apis.prod = apis ?? this._apis.prod; return this; }
    public setDevApis(apis: object): this { this._apis.dev = apis ?? this._apis.dev; return this; }
    public setTestApis(apis: object): this { this._apis.test = apis ?? this._apis.test; return this; }
    public setLocalApis(apis: object): this { this._apis.local = apis ?? this._apis.local; return this; }

    private _apis = { prod: {}, test: {}, local: {}, dev: {} };

    public showCover(api?: string): void {
        if (!!api && this.uncovers.contains(api)) return;
        const cover = UIManager.instance.netCover;
        if (!!cover) cover.show();
    }

    public hideCover(api?: string): void {
        if (!!api && this.uncovers.contains(api)) return;
        const cover = UIManager.instance.netCover;
        if (!!cover) cover.hide();
    }
}